<template>
  <div class="channel-edit">
    <!-- 我的频道 -->
    <van-cell :border="false">
      <div slot="title" class="title-text">我的频道</div>
      <van-button
        type="danger"
        plain
        round
        size="small"
        @click="isEdit = !isEdit"
      >{{ isEdit ? '完成': '编辑' }}</van-button>
    </van-cell>
    <van-grid square :gutter="10">
      <van-grid-item
        class="grid-item"
        v-for="(channel, i) in myChannels"
        :key="i"
        @click="onMyChannelClick(channel, i)"
      >
        <van-icon v-show="isEdit && i!==0" name="clear" slot="icon"/>
        <span class="text" :class="{active: i === active}" slot="text">{{channel.name}}</span>
      </van-grid-item>
    </van-grid>

    <!-- 频道推荐 -->
    <van-cell :border="false">
      <div slot="title" class="title-text">频道推荐</div>
    </van-cell>
    <van-grid class="recommend-grid" square :gutter="10">
      <van-grid-item
        class="grid-item"
        v-for="(channel, index) in recommendChannels"
        :key="index"
        :text="channel.name"
        icon="plus"
        @click="onAddChannel(channel)"
      />
    </van-grid>
  </div>
</template>

<script>
export default {
  props: {
    myChannels: {
      type: Array,
      required: true
    },
    active: {
      type: Number,
      required: true
    },
    allChannels: {
      type: Array,
      required: true
    }
  },
  data() {
    return {
      isEdit: false // 控制编辑状态的显示
    };
  },

  mounted() {},

  methods: {
    onAddChannel(channel) {
      this.myChannels.push(channel);
    },
    // 点击我的频道
    onMyChannelClick(channelItem, index) {
      if (this.isEdit) {
        // 执行删除操作
        // 判断是否为推荐
        if (index === 0) return;
        if (index <= this.active) {
          this.$emit("update-active", this.active - 1, true);
        }
        this.myChannels.splice(index, 1);
      } else {
        // 执行跳转操作
        this.$emit("update-active", index, false);
      }
    }
  },
  computed: {
    // recommendChannels() {
    //   return this.allChannels.filter(channel => {
    //     const mychannel = this.myChannels.find(myChannel => {
    //       return myChannel.id === channel.id;
    //     });
    //     return !mychannel;
    //   });
    // },
    recommendChannels() {
      return this.allChannels.filter(channel => {
        return !this.myChannels.some(myChannel => myChannel.id === channel.id);
      });
    }
  }
};
</script>

<style lang="less" scoped>
.channel-edit {
  padding: 85px 0;
  .title-text {
    font-size: 32px;
    color: #333;
  }
  ::v-deep .grid-item {
    padding-top: 130px !important;
    width: 160px;
    height: 86px;
    .van-grid-item__content {
      height: 90px !important;
      border-radius: 37px;
      white-space: nowrap;
      background-color: #f4f5f6;
      .van-grid-item__text,
      .text {
        color: #222;
        font-size: 28px;
        margin-top: 0;
      }
      .active {
        color: red;
      }
      .van-grid-item__icon-wrapper {
        position: unset;
      }
    }
  }
}
.van-button {
  width: 120px;
  height: 50px;
  font-size: 26px;
  color: #f85959;
}
/deep/ .recommend-grid {
  .van-grid-item__content {
    flex-direction: row;
    color: #222;
    .van-icon {
      font-size: 24px;
      margin-right: 6px;
    }
    .van-grid-item__text {
      font-size: 28px;
      margin-top: 0;
    }
  }
}
/deep/ .van-icon-clear {
  position: absolute;
  right: -10px;
  top: -10px;
  font-size: 36px;
  color: #cacaca;
  z-index: 2;
}
::v-deep .van-grid-item__content::after,
.van-grid-item__content--surround::after {
  border-width: 0;
}
</style>